//年份
var currentYear=new Date().getFullYear();
var years = [currentYear-5, currentYear-4, currentYear-3, currentYear-2, currentYear-1 ];
//chartname
var categoryChart = null;
var deviceChart = null;
var provinceChart = null;
var proDefectPartChart;
var proDefectCauseChart;
var secDefectPartChart;
var secDefectCauseChart;
var channelDefPartChart;
var channelDefCauseChart;
//初始情况下显示years[0]年
getDataByYear(years[0]);
//timeline
var ProtectTimeLine;
var selectedYear=years[0];
initProtectTimeLine();


function getDataByYear(y){
	$.getJSON('/rest/defect/getDefectAnalysisByYear/'+y, function(result) {
		if(!$.isEmptyObject(result)){
			$('.totalNum').html(result.count);
			$('.reality').html(result.reality);
			$('.rate').html(result.rate);
			$('.timelyRate').html(result.timelyRate);
			$('.proNum').html(result.details[0].count);
			$('.proRate').html(result.details[0].rate);
			$('.secNum').html(result.details[1].count);
			$('.secRate').html(result.details[1].rate);
			$('.channelNum').html(result.details[2].count);
			$('.channelRate').html(result.details[2].rate);
			drawChart(result);
		}else{
			$('.totalNum').html(0);
		}
	});
}

function drawChart(data){
	//缺陷类别
	categoryChart = initPieChart('category', data.category, '年缺陷类别分布');
	//缺陷设备
	deviceChart = initPieChart('device', data.device, '年缺陷设备分布');
	//省区
	initProvinceChart(data);
	//保护装置
	proDefectPartChart=initPieChart('protectorDefPosition',data.details[0].values[0],'年缺陷部位分布');
	proDefectCauseChart=initPieChart('protectorDefCause',data.details[0].values[1],'年缺陷原因分布');
	//二次回路
	secDefectPartChart=initPieChart('secondDefPosition',data.details[1].values[0],'年缺陷部位分布');
	secDefectCauseChart=initPieChart('secondDefCause',data.details[1].values[1],'年缺陷原因分布');
	//保护通道
	channelDefPartChart=initPieChart('channelDefPosition',data.details[2].values[0],'年缺陷部位分布');
	channelDefCauseChart=initPieChart('channelDefCause',data.details[2].values[1],'年缺陷原因分布');
	
}

function initProtectTimeLine() {
	var timeLine = new TimeLine("protectTimeLine");
	var myopt = timeLine.getOption();
	 myopt.baseOption.timeline.loop = true;
	 timeLine.setTimeDatas(years);
	 timeLine.show();
	 timeLine.setTimeLineChangeListner(function(selectedYearIndex){
		 selectedYear = years[selectedYearIndex];
		 $('.info-box-content .year').html(selectedYear);
		 getDataByYear(selectedYear);
	 });
}


function initPieChart(chartid, data, title) {
	var pie = new Pie(chartid);
	pie.setPie(data);
	var option = pie.getOption();
	option.title.textStyle.fontSize=16;
	option.title.textStyle.fontWeight='normal';
	option.legend.orient='vertical';
	option.series[0].label.normal.formatter='{d}%';
	pie.setPieOuterRadius('50%');
	pie.setLegendPosition('left','bottom');
	pie.setTitle(selectedYear+title);
	pie.setTitlePosition('center');
	pie.show();
	return pie;
}
function initProvinceChart(data) {
	var bar = new CategoryBar('province');
	bar.setxAxis(data.province.lables);
	bar.setyAxis(data.province.values);
	bar.setyAxisName("缺陷次数");
	bar.setTitle(selectedYear+'年缺陷省区分布');
	bar.setTitlePosition('center');
	var option = bar.getOption();
	option.legend.show = false;
	option.title.textStyle.fontSize=16;
	option.title.textStyle.fontWeight='normal';
	bar.show();
	provinceChart = bar.getChart();
	return provinceChart;
}


